package com.fyzs.animation

import android.animation.ValueAnimator
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.view.View
import android.view.animation.AlphaAnimation
import android.view.animation.Animation
import android.view.animation.TranslateAnimation
import android.widget.Button
import android.widget.LinearLayout
import android.widget.Toast
import androidx.core.view.ViewCompat
import kotlin.math.abs
import kotlin.math.roundToInt

class MainActivity : AppCompatActivity(), View.OnClickListener {


    lateinit var reject: View
    lateinit var silence: View
    lateinit var dialpad: View
    lateinit var accept: View
    lateinit var phone: View

    val TAG = "MainActivity"
    lateinit var resetButton: Button
    lateinit var startButton: Button


    var step = 0

    val ANIMATION_DURATION = 400L

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        reject = findViewById(R.id.red_button)
        phone = findViewById(R.id.grey_button)
        dialpad = findViewById(R.id.yellow_button)
        accept = findViewById(R.id.green_button)
        silence = findViewById(R.id.blue_button)

        resetButton = findViewById(R.id.reset_button)
        startButton = findViewById(R.id.start_button)


        reject.setOnClickListener(this)
        phone.setOnClickListener(this)
        dialpad.setOnClickListener(this)
        accept.setOnClickListener(this)
        silence.setOnClickListener(this)

        dialpad.visibility = View.GONE
        silence.visibility = View.GONE



        resetButton.setOnClickListener {
            reset()
        }
        startButton.setOnClickListener {

                    accept.visibility = View.GONE
                    reject.visibility = View.VISIBLE
                    dialpad.visibility = View.VISIBLE
                    silence.visibility = View.VISIBLE
                    phone.visibility = View.VISIBLE
                    valueAnimation.start()
        }
    }

    private fun reset() {
        //设置
        step = 0
        accept.visibility = View.VISIBLE
        reject.visibility = View.VISIBLE
        dialpad.visibility = View.GONE
        silence.visibility = View.GONE
        phone.visibility = View.VISIBLE

        Log.i(TAG, "widht : ${dialpad.width}")
    }

    val valueAnimation = ValueAnimator.ofFloat(-1f, 0f).apply {
        duration = ANIMATION_DURATION
        addUpdateListener {
            val p = it.animatedValue as Float
            Log.i(TAG, "animator update :${p} ")

            val silenceLps = silence.layoutParams as LinearLayout.LayoutParams
            silenceLps.marginEnd = (p*silence.width).roundToInt()
            silence.alpha = abs(1+p)

            silence.layoutParams = silenceLps

            val dialpadLps = dialpad.layoutParams as LinearLayout.LayoutParams
            dialpadLps.marginStart = (p*dialpad.width).roundToInt()
            dialpad.layoutParams = dialpadLps

            dialpad.alpha = abs(1+p)



        }
    }


    override fun onClick(v: View?) {
        Toast.makeText(
            this,
            when (v) {
                accept -> {
                    "accept"
                }
                reject -> {
                    "reject"
                }
                silence -> {
                    "silence"
                }
                dialpad -> {
                    "dialpad"
                }
                phone -> {
                    "phone"
                }
                else -> ""
            }, Toast.LENGTH_SHORT
        ).show()
    }


}